<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Constant keyword datatype | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'constant-keyword.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/constant-keyword.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/constant-keyword.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/constant-keyword.html" rel="nofollow" target="_blank">../en/constant-keyword.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">Mapping</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">Field datatypes</a></span>
»
<span class="breadcrumb-node">Constant keyword datatype</span>
</div>
<div class="navheader">
<span class="prev">
<a href="shape.html">« Shape datatype</a>
</span>
<span class="next">
<a href="mapping-fields.html">Meta-Fields »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="constant-keyword"></a>Constant keyword datatype<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/constant-keyword.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>

<p>Constant keyword is a specialization of the <a class="xref" href="keyword.html" title="Keyword datatype"><code class="literal">keyword</code></a> field for
the case that all documents in the index have the same value.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT logs-debug
{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "message": {
        "type": "text"
      },
      "level": {
        "type": "constant_keyword",
        "value": "debug"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/726.console"></div>
<p><code class="literal">constant_keyword</code> supports the same queries and aggregations as <code class="literal">keyword</code>
fields do, but takes advantage of the fact that all documents have the same
value per index to execute queries more efficiently.</p>
<p>It is both allowed to submit documents that don’t have a value for the field or
that have a value equal to the value configured in mappings. The two below
indexing requests are equivalent:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST logs-debug/_doc
{
  "date": "2019-12-12",
  "message": "Starting up Elasticsearch",
  "level": "debug"
}

POST logs-debug/_doc
{
  "date": "2019-12-12",
  "message": "Starting up Elasticsearch"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/727.console"></div>
<p>However providing a value that is different from the one configured in the
mapping is disallowed.</p>
<p>In case no <code class="literal">value</code> is provided in the mappings, the field will automatically
configure itself based on the value contained in the first indexed document.
While this behavior can be convenient, note that it means that a single
poisonous document can cause all other documents to be rejected if it had a
wrong value.</p>
<p>Before a value has been provided (either through the mappings or from a
document), queries on the field will not match any documents. This includes
 <a class="xref" href="query-dsl-exists-query.html" title="Exists query"><code class="literal">exists</code></a> queries.</p>
<p>The <code class="literal">value</code> of the field cannot be changed after it has been set.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="constant-keyword-params"></a>Parameters for constant keyword fields<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/constant-keyword.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following mapping parameters are accepted:</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<a class="xref" href="mapping-field-meta.html" title="meta"><code class="literal">meta</code></a>
</p>
</td>
<td valign="top">
<p>
Metadata about the field.
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">value</code>
</p>
</td>
<td valign="top">
<p>
The value to associate with all documents in the index. If this parameter
is not provided, it is set based on the first document that gets indexed.
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="shape.html">« Shape datatype</a>
</span>
<span class="next">
<a href="mapping-fields.html">Meta-Fields »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>